const Router = require('koa-router');
const axios = require('./utils/axios');

let router = new Router({
  prefix: '/search'
})

const sign = 'b735ab0b3e46d12ec07336ec5195a95f'

//搜索框输入数据时请求返回
router.get('/top', async (ctx) => {
  let {
    status,
    data: {
      top
    }
  } = await axios.get(`http://cp-tools.cn/search/top`, {
    //用户输入关键词/城市/签名
    params: {
      input: ctx.query.input,
      city: ctx.query.city,
      sign
    }
  })

  if (status === 200) {
    ctx.body = {
      top
    }
  } else {
    ctx.body = {
      top: []
    }
  }
})

//热词输入数据时请求返回
router.get('/hotSearch', async (ctx) => {
  let {
    status,
    data: {
      result
    }
  } = await axios.get(`http://cp-tools.cn/search/hotPlace`, {
    params: {
      city: ctx.query.city,
      sign
    }
  })

  if (status === 200) {
    ctx.body = {
      result
    }
  } else {
    ctx.body = {
      result: []
    }
  }
})

//Tab栏切换请求返回
router.get('/resultsByKeywords', async (ctx) => {
  let {
    status,
    data: {
      count,
      pois
    }
  } = await axios.get(`http://cp-tools.cn/search/resultsByKeywords`, {
    params: {
      city: ctx.query.city,
      keyword: ctx.query.keyword,
      sign
    }
  })

  if (status === 200) {
    ctx.body = {
      count,
      pois
    }
  } else {
    ctx.body = {
      count: 0,
      pois: []
    }
  }
})

router.get('/products', async (ctx) => {
  let keyword = ctx.query.keyword || '旅游';
  let city = ctx.query.city || '北京';
  let {
    status,
    data: {
      product,
      more
    }
  } = await axios.get('http://cp-tools.cn/search/products', {
    params: {
      keyword,
      city,
      sign
    }
  })

  if (status === 200) {
    ctx.body = {
      product,
      more: ctx.isAuthenticated() ? more : [],
      login: ctx.isAuthenticated()
    }
  } else {
    ctx.body = {
      product: {},
      more: ctx.isAuthenticated() ? more : [],
      login: ctx.isAuthenticated()
    }
  }
})

module.exports = router;
